<?php
class VADFileAttr{
    private $ori_path;
    private $des_path;
    private $vad_path;
    private $speech_id;
    public function __construct(Array $attr)
    {
        $this->validateAndInit($attr);
    }

    public function getOriginalPath(){
        return $this->ori_path;
    }

    public function getDestinationPath(){
        return $this->des_path;
    }

    public function getVadPath(){
        return $this->vad_path;
    }

    public function getDestinationJsonPath(){
        if(!$this->des_path){
            return '';
        }
        $des_json_path = pathinfo($this->des_path)['dirname'] . "/" . pathinfo($this->des_path)['filename'] . ".json";
        return $des_json_path;
    }

    public function getSpeechId(){
        return $this->speech_id;
    }

    private function validateAndInit($attr){
        $this->setOriginalPath($attr['ori_path']);
        $this->setDestinationPath($attr['des_path']);
        $this->setVadPath($attr['vad_path']);
        $this->setSpeechId($attr['id']);
    }

    private function setOriginalPath($ori_path){
        if(!is_file($ori_path)){
            throw new \Exception("FILE '$ori_path' NOT EXIST");
        }
        if(!$this->extJudge(pathinfo($ori_path)['extension'])){
            throw new \Exception("file '$ori_path' extension error");
        }
        $this->ori_path = $ori_path;
    }

    private function extJudge($ext)
    {
        $exts = ['wav', 'pcm', 'm4a', 'amr', 'mp3', 'ogg'];
        return in_array(strtolower($ext), $exts);
    }

    private function setDestinationPath($des_path){
        if(!is_dir(pathinfo($des_path)['dirname'])){
            mkdir(pathinfo($des_path)['dirname'], 0777, true);
        }
        $this->des_path = $des_path;
    }

    private function setVadPath($vad_path){
        if(!is_dir(pathinfo($vad_path)['dirname'])){
            throw new \Exception("DIR '$vad_path' NOT EXIST");
        }
        $this->vad_path = $vad_path;
    }

    private function setSpeechId($id){
        if(!$id){
            throw new \Exception("Speech id: '$id' NOT EXIST");
        }
        $this->speech_id = $id;
    }
}